package com.example.mywatermask;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Base64;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class StartActivity extends AppCompatActivity
        implements View.OnClickListener, TextView.OnEditorActionListener {
    private Button mBtn1;
    private Button mBtn2;
    private Button mBtn3;
    private EditText mEt;
    private EditText mEt1;
    private EditText mEt2;
    private EditText mEt3;
    private EditText mEt4;
    private EditText mEt5;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);

        mEt1 = findViewById(R.id.et1);
        mEt2 = findViewById(R.id.et2);
        mEt3 = findViewById(R.id.et3);
        mEt4 = findViewById(R.id.et4);
        mEt5 = findViewById(R.id.et5);
        mEt1.setOnEditorActionListener(this);
        mEt2.setOnEditorActionListener(this);
        mEt3.setOnEditorActionListener(this);
        mEt4.setOnEditorActionListener(this);
        mEt5.setOnEditorActionListener(this);

        mBtn1 = findViewById(R.id.btn1);
        mBtn2 = findViewById(R.id.btn2);
        mBtn3 = findViewById(R.id.btn3);
        mBtn1.setOnClickListener(this);
        mBtn2.setOnClickListener(this);
        mBtn3.setOnClickListener(this);
        mEt = findViewById(R.id.et);
        mEt.setOnEditorActionListener(this);
        mEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String trim = s.toString().trim();
                if (trim.isEmpty()) {
                    return;
                }
                System.out.println("s==========================" + trim);
                try {
                    String encode = URLEncoder.encode(trim, "UTF-8");
                    System.out.println("encode===================" + encode);
                    String decode = URLDecoder.decode(encode, "UTF-8");
                    System.out.println("decode=====================" + decode);
                    //
                    String encode2 = Base64.encodeToString(trim.getBytes(), Base64.DEFAULT);
                    String decode2 = new String(Base64.decode(encode2, Base64.DEFAULT));
                    System.out.println("encode2=====================" + encode2);
                    System.out.println("decode2=====================" + decode2);
                    mBtn2.setText(decode);
                    mBtn3.setText(decode2);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn1:
                startActivity(new Intent(StartActivity.this, MainActivity.class));
                break;
            case R.id.btn2:
                startActivity(new Intent(StartActivity.this, MainActivity2.class));
                break;
            case R.id.btn3:
                break;
        }
    }

    //actionNone : 回车键，按下后光标到下一行
    //actionGo ： Go，
    //actionSearch ： 放大镜
    //actionSend ： Send
    //actionNext ： Next
    //actionDone ： Done，确定/完成，隐藏软键盘，即使不是最后一个文本输入框
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean isOK = true;
        switch (actionId) {
            case EditorInfo.IME_ACTION_NONE:
                System.out.println("点击-->NONE");
                break;
            case EditorInfo.IME_ACTION_GO:
                System.out.println("点击-->GO");
                break;
            case EditorInfo.IME_ACTION_SEARCH:
                System.out.println("点击-->SEARCH");
                break;
            case EditorInfo.IME_ACTION_SEND:
                System.out.println("点击-->SEND");
                break;
            case EditorInfo.IME_ACTION_NEXT:
                System.out.println("点击-->NEXT");
                break;
            default:
                isOK = false;
                break;
        }
        return isOK;
    }
}
